#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1e9 + 7;

int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}

ll qmi(ll a, ll b)
{
    ll res = 1;
    while(b)
    {
        res = res * a % mod;
        b >>= 1;
        a = a * a % mod;
    }
    return res;
}

ll func(ll x, ll y)
{
    int t = y - x + 1;
    return ((x + y) * t) / 2;
}

void solve()
{
    ll n, k;
    cin >> n >> k;
    ll l = k, r = n + k - 1;
    ll sum = ((l + r) * n) / 2;
    ll x = sum / 2;

    while(l < r)
    {
        ll mid = (l + r + 1) / 2;
        ll ju = func(k, mid);
        if(ju <= x) l = mid;
        else r = mid - 1;
    }
    ll a = func(k, l), b = func(l + 1, n + k - 1);
    ll c = func(k, l + 1), d = func(l + 2, n + k - 1);

    long long ans = min(abs(a - b), abs(c - d));
    cout << ans << endl;

}

int main()
{
    int t;
    cin >> t;
    while(t--) solve();

    return 0;
}